﻿
// First: Create config
ko.validation.configure({
	registerExtenders: true,
	messagesOnModified: true,
	insertMessages: true,
	parseInputAttributes: true,
	messageTemplate: null,
	decorateElement: true,
	errorClass: "error",
	errorMessageClass: "error-desc"
});

// Second: Create new model to apply to view
var viewModel = new function () {
    // Options
    this.OptionID = ko.observable();
    this.OptionList = ko.observableArray(data.ListBannerOption);
    this.GroupID = ko.observable();
    this.GroupList = ko.observableArray(data.ListBannerGroup);
    this.PaymentID = ko.observable();
    this.PaymentList = ko.observableArray(data.ListBannerPayment);
    this.PositionID = ko.observable(data.ListBannerPosition[1].ID);
    this.PositionList = ko.observableArray(data.ListBannerPosition);

    // About you
    this.Title = ko.observable().extend({ maxLength: 250 }).extend({ required: { message: "Tên quảng bá là bắt buộc"} });
    this.ShortDesc = ko.observable();
    this.LongDesc = ko.observable();
    this.ImageURL = ko.observable();
    this.FlashURL = ko.observable();
    this.VideoURL = ko.observable();
    this.HTML = ko.observable();
    this.WebsiteUrl = ko.observable();
    this.ShowAllPages = ko.observable(false);
    this.ShowOnHomePage = ko.observable(false);
    this.ShowOnCategoryPage = ko.observable(false);
    this.ShowOnZonePage = ko.observable(false);
    this.ShowOnDetailPage = ko.observable(false);
    this.ShowOnOtherPage = ko.observable(false);
    this.ListPrice = ko.observable(0);
    this.UnitPrice = ko.observable("vnd");
    this.Discount = ko.observable(0);
    this.Active = ko.observable(false);

    this.ImageFileName = ko.observable();
    this.FlashFileName = ko.observable();
    this.VideoFileName = ko.observable();

    this.Width = ko.observable(300);
    this.Height = ko.observable(225);

    // Submit, Next button
    this.InvalidateForm = ko.observable(false);
    this.SubmitForm = function () {
        if (viewModel.errors().length == 0) {
            $('form').submit();
        }
        else {
            viewModel.errors.showAllMessages();
            this.InvalidateForm(true);
        }
    }
};

viewModel.errors = ko.validation.group(viewModel);

// Third: Apply view model to view to handle action when user input data
ko.validation.rules.pattern.message = 'Invalid.';
ko.applyBindings(viewModel, document.getElementById("main"));

$(document).ready(function () {
    $(".calendar-date").datepicker();
});